<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require_once '/application/libraries/qiniusdk/autoload.php';
// 引入鉴权类
use Qiniu\Auth;
// 引入上传类
use Qiniu\Storage\UploadManager;
class Deviceapi extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->library('session');
		$this->load->model('Agent_m');
		$this->load->model('Adminlog_m');
		$this->load->model('Config_m');
		$this->load->model('Device_m');
		$this->load->model('Commission_m');
		$this->load->model('Order_m');
		$this->load->library('api/agentapi_service');
		ini_set('date.timezone','Asia/Shanghai');
		//header('Content-Type: application/json;charset=utf-8');
	}

	public function device_api(){
    	switch ($_SERVER["REQUEST_METHOD"]) {
			case 'GET':
				/**
			     * 通过设备id/设备编号查设备或全查设备
			     */
				$device_id=$this->input->get('device_id');//设备id
				$device_sn=$this->input->get('device_sn');//设备编号
				if($device_id){
					$this->getDeviceByDeviceid();//通过设备id获取单个设备信息
				}else if($device_sn){
					$this->getDeviceByDevicesn();//通过设备编号获取单个设备信息
				}else{
					$this->getDeviceList();//获取设备列表					
				}
				break;				
			case 'POST':
				$this->addDevice();//添加设备
				break;
			case 'PUT':
				$this->updateDeviceUseridByDeviceid();//修改设备信息
				break;
			case 'DELETE':
				# code...
				break;
			default:
				# code...
				break;
		}
	}


	/**
     * 修改设备信息
     * @param device_id     设备id
     * @param user_id     	设备绑定的用户id
     * @param agent1     	设备所属一级代理
     * @param agent2     	设备所属二级代理
     */
	public function updateDeviceUseridByDeviceid(){
		if(IS_PUT){
			$device_id = $this->input->input_stream('device_id');
			$user_id = $this->input->input_stream('user_id');
			$agent1 = $this->input->input_stream('agent1');
			$agent2 = $this->input->input_stream('agent2');
			if(empty($device_id)){
				$data['success'] = error('4008');	    	   	
			    echo _echo($data);
			    exit;
			}
			$this->load->model('Device_m');
			$device_where['device_id']=$device_id;
			if($user_id){
				$data['user_id']=$user_id;
			}
			if($agent1){
				$data['agent1']=$agent1;
			}
			if($agent2){
				$data['agent2']=$agent2;
			}
			$this->Device_m->update($device_where,$data);
			$Device=$this->Device_m->select($device_where);
			$Device_data['success'] = success('4000');	  
			$Device_data['Device'] = $Device;  
		    echo _echo($Device_data);
		    exit;
		}else{
			$data['success'] = error('4008');	    	   	
		    echo _echo($data);
		    exit;
		}
	}



    /**
     * 添加设备 接收设备编号数组
     * @param devicesn_list        设备编号数组
     */
	public function addDevice(){
		if(IS_POST){

			set_time_limit(3000);
			$devicesn_list=$this->input->post('devicesn_list');//设备编号数组
			
			if(empty($devicesn_list)){
				$data['success'] = error('4008');	    	   	
			    echo _echo($data);
			    exit;
			}
			
			$devicesn_list=json_decode($devicesn_list);
			$where['inc_type']='qiniu';
			$qiniu  = $this->Config_m->getconfig($where);
			$Device_data['success'] = success('4000');
			foreach ($devicesn_list as $key => $value) {
				$Device_data['device'][]=$this->do_addone($value,$qiniu);
			}				  
	    	echo _echo($Device_data);
	    	exit;
		}else{
			$data['success'] = error('4008');	    	   	
		    echo _echo($data);
		    exit;
		}
		
	}

	/**
     * 添加设备 接收设备编号数组
     * @param devicesn_list        设备编号数组
     */
	public function add_to_device(){
			$devicesn=$this->input->get('devicesn');//设备编号数组
			//$devicesn_list=json_decode($devicesn);
			$where['inc_type']='qiniu';
			$qiniu  = $this->Config_m->getconfig($where);
			$Device_data['success'] = success('4000');
			$Device_data['device'][]=$this->do_addone($devicesn,$qiniu);  
	    	echo _echo($Device_data);

	    	exit;
	}

	/**
     * 执行添加单个设备 接收设备编号数组
     * @param value        设备编号
     * @param qiniu        七牛参数
     */
	public function do_addone($value,$qiniu){
		$device_sn=$value;
		//$data=$this->chaxun_to_device($device_sn);
		//if($data['result']==70006||$data['result']==70007||$data['result']==0){
			$Device_where = array('device_sn'=>$device_sn); //where
			//查询有没有这个设备
			$Device_old = $this->Device_m->select($Device_where);
			if($Device_old){
				return $Device_old['0']['QRcode'];
		    	exit;
			}else{
				$Device['device_sn'] = $device_sn;
				$Device['addtime'] = date("YmdHis");
				//生成二维码
				$url='http://'.$_SERVER['SERVER_NAME'].'/mobile/login.html?device_sn='.$device_sn;
				$img='http://qr.liantu.com/api.php?text='.$url;
	        	$logo='uploads/logo.png';
	        	$name='uploads/QCcode/'.$device_sn.'.png';
	        	$this->mark_photo($img,$device_sn,$logo,$name);

	        	//上传七牛云存储 需要填写你的 Access Key 和 Secret Key
		        //$accessKey = 'MNS70nLctMOm0QVBC6Gq5pk9w7CARLh8YqFppXoH';
		        //$secretKey = 'SQvrFDYKWh6YBtILrXHwFbH3wfKbsF871sEGOXmq';
		        $accessKey=$qiniu['accessKey'];
        		$secretKey=$qiniu['secretKey'];
		        // 构建鉴权对象
		        $auth = new Auth($accessKey, $secretKey);
		        // 要上传的空间
		        //$bucket = 'mifi';
		        $bucket=$qiniu['bucket'];
		        // 生成上传 Token
		        $token = $auth->uploadToken($bucket);
		        // 要上传文件的本地路径
		        $filePath = $name;
		        // 上传到七牛后保存的文件名
		        $key = $device_sn.'.png';
		        // 初始化 UploadManager 对象并进行文件的上传。
		        $uploadMgr = new UploadManager();
		        // 调用 UploadManager 的 putFile 方法进行文件的上传。
		        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
		        
		        //if(file_exists($name)){
			    //    unlink($name);
			    //}

		        //$Device['QRcode'] = 'http://p1e7rmjtq.bkt.clouddn.com/'.$device_sn.'.png';       
		        $Device['QRcode'] = $qiniu['qiniuurl'].$device_sn.'.png';	        
				$new_Device = $this->Device_m->insert($Device);//添加设备
				$result['code']=4000;
				$result['msg']="添加成功";
				$result['device_sn']=$Device['device_sn'];
				$result['imgurl']=$Device['QRcode'];
				return $result;   
				exit;
			}
		//}else{
		//	$result['code']=4010;
		//	$result['msg']="设备出厂未录入数据";
		//	$result['device_sn']=$device_sn;
		//	return $result;
		//	exit;
		//}
		
	}

	/**
     * 获取全部设备列表
     * @param binduser     是否绑定用户 1已绑  2没绑  没传返还全部
     * @param page         页码
     * @param limit         每页显示数量
     */
	public function getDeviceList(){
		if(IS_GET){
			$binduser=$this->input->get('binduser');
			$page = $this->input->get('page');
	        if(empty($page)){
	        	$page=1;
	        }
	        $limit = $this->input->get('limit');
	        if(empty($limit)){
	        	$limit=5;
	        }
			if(empty($page)||empty($limit)){
				$data['success'] = error('4008');	    	   	
			    echo _echo($data);
			    exit;
			}
			$offset=($page-1)*$limit;
			$where=array();
			if($binduser==1){
				$where='user_id is not null';
			}elseif($binduser==2){
				$where='user_id is null';
			}
			$DeviceList=$this->Device_m->select($where,'device_id desc',"$limit,$offset");
			$Device_data['success'] = success('4000');	  
			$Device_data['DeviceList'] = $DeviceList;  
		    echo _echo($Device_data);
		    exit;
	    }else{
			$data['success'] = error('4008');	    	   	
		    echo _echo($data);
		    exit;
		}
	}

	/**
     * 通过设备id获取单个设备信息
     * @param device_id     设备id
     */
	public function getDeviceByDeviceid(){
		if(IS_GET){
			$device_id=$this->input->get('device_id');
			if(empty($device_id)){
				$data['success'] = error('4008');	    	   	
			    echo _echo($data);
			    exit;
			}
			$where['device_id']=$device_id;
			$Device=$this->Device_m->select($where);
			$Device_data['success'] = success('4000');	  
			$Device_data['Device'] = $Device;  
		    echo _echo($Device_data);
		    exit;
	    }else{
			$data['success'] = error('4008');	    	   	
		    echo _echo($data);
		    exit;
		}
	}


	/**
     * 通过设备编号获取单个设备信息
     * @param device_sn     设备编号
     */
	public function getDeviceByDevicesn(){
		if(IS_GET){
			$this->load->model('Device_m');
			$device_sn=$this->input->get('device_sn');
			if(empty($device_sn)){
				$data['success'] = error('4008');	    	   	
			    echo _echo($data);
			    exit;
			}
			$where['device_sn']=$device_sn;
			$Device=$this->Device_m->select($where);
			if($Device){
				$Device_data['success'] = success('4000');	  
				$Device_data['Device'] = $Device;  
			    echo _echo($Device_data);
			    exit;
			}else{
				$Device_data['success'] = error('4006');
			    echo _echo($Device_data);
			    exit;
			}			
	    }else{
			$data['success'] = error('4008');	    	   	
		    echo _echo($data);
		    exit;
		}
	}


	/**
     * 通过用户id获取设备信息
     * @param user_id     用户id
     */
	public function getDeviceByuserid(){
		if(IS_GET){
			$sess_user=$this->session->userdata('user');
			
			if($sess_user){				
				
				$where['user_id']=$sess_user[0]['user_id'];
				$Device=$this->Device_m->select($where);
				
				$Device_data['success'] = success('4000');	  
				$Device_data['Device'] = $Device;
			    echo _echo($Device_data);
			    exit;
			}else{
				$data['success'] = error('4008');	    	   	
			    echo _echo($data);
			    exit;
			}
	    }else{
			$data['success'] = error('4008');	    	   	
		    echo _echo($data);
		    exit;
		}
	}


	/**
     * 获取某个代理的设备信息
     * @param agentid     代理id
     */
	public function getDeviceByAgentid(){
		if(IS_GET){
			$agentid=$this->input->get('agentid');//代理id
			if(empty($agentid)){
				$data['success'] = error('4008');	    	   	
			    echo _echo($data);
			    exit;
			}
			$agent=$this->Agent_m->select(array('id'=>$agentid));
		    if(empty($agent)){
		        $data['success'] = error('4011');
		        echo _echo($data);
		        exit;
		    }else{
		    	$agent=$agent[0];
		    }
		    if($agent['agent_level']=='1'){
				$where['agent1']=$agent['id'];
			}elseif($agent['agent_level']=='2'){
				$where['agent2']=$agent['id'];
			}
		    $page = $this->input->get('page');
		    if(empty($page)){
		    	$page=1;
		    }
		    $limit = $this->input->get('limit');
		    if(empty($limit)){
		    	$limit=5;
		    }
		    $jihuo = $this->input->get('jihuo');
		    
		    $data['count']=$this->Device_m->getDevicecount($where);
		    if($jihuo==='0'){
		    	$where['jihuo']=0;
		    }elseif($jihuo==='1') {
		    	$where['jihuo']=1;
		    }
		    $data['success'] = success('4000');
		    $deviceList=$this->Device_m->getList($where,$page,$limit);
		    if($deviceList){
			    foreach ($deviceList as $key => $value) {
		        	//$deviceList[$key]['taocan']=$this->Commission_m->getLastCommission($value['device_sn']);
		        	$deviceList[$key]['taocan']=$this->Order_m->getLastOrder($value['device_sn']);
		        }  
		    }      
		    $data['deviceList']=$deviceList;        
		    echo _echo($data);
		    exit;
	    }else{
			$data['success'] = error('4008');	    	   	
		    echo _echo($data);
		    exit;
		}
	}


	/**
     * 返回七牛云存储token
     */
	public function qiniutoken(){
		$where['inc_type']='qiniu';
		$qiniu  = $this->Config_m->getconfig($where);
		//上传七牛云存储 需要填写你的 Access Key 和 Secret Key
        //$accessKey = 'MNS70nLctMOm0QVBC6Gq5pk9w7CARLh8YqFppXoH';
        //$secretKey = 'SQvrFDYKWh6YBtILrXHwFbH3wfKbsF871sEGOXmq';
        $accessKey=$qiniu['accessKey'];
        $secretKey=$qiniu['secretKey'];
        // 构建鉴权对象
        $auth = new Auth($accessKey, $secretKey);
        // 要上传的空间
        //$bucket = 'mifi';
        $bucket = $qiniu['bucket'];
        // 生成上传 Token
        $token = $auth->uploadToken($bucket);
        echo json_encode($token);
        exit;
	}



	//拼接图片
    private function mark_photo($background,$text,$code,$name){                
        $back=imagecreatefrompng($background);            
        $color=imagecolorallocate($back,0,0,0);           
        $code=imagecreatefrompng($code);           
        $code_w=imagesx($code);
        $code_h=imagesy($code);    
        $font="mobile/font/MSYH.ttf";           
        //imagettftext只认utf8字体，所以用iconv转换
        imagettftext($back,15,0,60,299,$color,$font,$text);        
        //执行合成调整位置
        imagecopyresampled($back, $code, 110,110, 0, 0, 70, 70, $code_w, $code_h);
        imagejpeg($back,$name);           
        imagedestroy($back);
        imagedestroy($code);
    }


    /**
     * post进行设备查询url请求
     * @param string $url
     * @param array $post_data
     */
    function chaxun_to_device($device_id) {
        $str_cookie = isset($ext_params['str_cookie']) ? $ext_params['str_cookie'] : '';$ch = curl_init();//初始化curl
      	$length = strlen($device_id);
		if($length != 16){
			$device_id = "0".$device_id."";
		}

		$get_device_info_url_api_where = array('name' => 'get_device_info_url');
		$get_device_info_uid_api_where = array('name' => 'uid');
		$get_device_info_signkey_api_where = array('name' => 'signkey');

		$get_device_info_url  = $this->Config_m->select($get_device_info_url_api_where);
		$uid  = $this->Config_m->select($get_device_info_uid_api_where);
		$signkey  = $this->Config_m->select($get_device_info_signkey_api_where);
		
        $get_device_info_url = $get_device_info_url[0]['value']; 
       	$uid = $uid[0]['value']; 
       	$signkey = $signkey[0]['value'];
       	
        $url = $get_device_info_url;

        $post_data['id']         = $uid;
        $post_data['ts']         = time();
        $post_data['device_id']  = $device_id;
		
        $sign = hash_hmac('sha512', _echo($post_data), $signkey);
        $header[] = 'Content-Type: application/x-www-form-urlencoded; charset=utf-8';
        $header[] = 'sign:'.$sign;
        $header[] = 'uid:'.$uid;
        $postUrl  = $url;
        $curlPost = _echo($post_data);
        
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
        curl_setopt($ch, CURLINFO_HEADER_OUT, true);
        curl_setopt($ch, CURLOPT_COOKIE, $str_cookie);
        //curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
        curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
        curl_setopt($ch, CURLOPT_POST, true);//post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        $datas = curl_exec($ch);//运行curl 当前套餐名称
        $request_header = curl_getinfo( $ch, CURLINFO_HEADER_OUT);
        curl_close($ch);
		$data = json_decode($datas, true);
		return $data;  
    }




    /**
     * 批量分配设备给某个代理
     * @param tooken     		代理临时通行证
     * @param agentid     		代理id
     * @param devicesnList     	设备编号列表
     */
    public function deviceToAgent2(){
    	$tooken = $this->input->post('tooken');
    	$agent2 = $this->input->post('agentid');
        $devicesnList = $this->input->post('devicesnList');
        if(empty($tooken)||empty($agent2)||empty($devicesnList)){
            $data['success'] = error('4008');
            echo _echo($data);
            exit;
        }
        $agentid=jiemi($tooken);
        $agent=$this->agentapi_service->getAgentById(array('id'=>$agentid));
        if(empty($agent)){
            $data['success'] = error('4011');
            echo _echo($data);
            exit;
        }
        foreach ($devicesnList as $key => $value) {
        	$device=$this->Device_m->select(array('device_sn'=>$value));
        	if($device[0]['agent2']){
        		$result[$value]=false;
        	}else{
        		$this->Device_m->update(array('device_sn'=>$value),array('agent2'=>$agent2,'fptime'=>date('Y-m-d H:i:s')));
        		$result[$value]=true;
        	}        	
        }        
        $Device_data['success'] = success('4000');	  
		$Device_data['result'] = $result;  
	    echo _echo($Device_data);
	    exit;
    }


	/**
     * 某个代理登录后获取自己设备列表
     * @param tooken     代理临时通行证
     * @param page       页码
     * @param limit      每页条数
     * @param jihuo      设备是否激活 0未激活 1激活 不传返回全部
     * @param fenpei     设备是否分配给了二级代理  0未分配 1已分配 不传返回全部
     */
	public function getSelfDeviceList(){
		$tooken = $this->input->get('tooken');
        if(empty($tooken)){
            $data['success'] = error('4008');
            echo _echo($data);
            exit;
        }
        $agentid=jiemi($tooken);
        $agent=$this->agentapi_service->getAgentById(array('id'=>$agentid));
        if(empty($agent)){
            $data['success'] = error('4011');
            echo _echo($data);
            exit;
        }
        $whereor=array();
        if($agent['agent_level']=='1'){
			$where['agent1']=$agent['id'];
			$data['count']=$this->Device_m->getDevicecount($where);
        	$data['count']['weifenpei']=$this->Device_m->getDeviceweifenpei($where);
        	//$data['count']['fenpei']=$this->Device_m->getDevicefenpei($where);
		}elseif($agent['agent_level']=='2'){
			$where['agent2']=$agent['id'];
			$data['count']=$this->Device_m->getDevicecount($where);
		}
        $page = $this->input->get('page');
        if(empty($page)){
        	$page=1;
        }
        $limit = $this->input->get('limit');
        if(empty($limit)){
        	$limit=10;
        }
        
        $jihuo = $this->input->get('jihuo');
        if($jihuo==='0'){
        	$where['jihuo']=0;
        }elseif($jihuo==='1') {
        	$where['jihuo']=1;
        }
        $fenpei = $this->input->get('fenpei');
        if($fenpei==='0'){
        	$where['jihuo']=0;
        	//$where['agent2']=null;
        }elseif($fenpei==='1') {
        	$where['jihuo']=1;
        	//$whereor="(jihuo = 1 and agent1 = ".$agent['id'].") ";
        }
        $data['success'] = success('4000');

        $deviceList=$this->Device_m->getList($where,$page,$limit,$whereor);
        if($deviceList){
    	    foreach ($deviceList as $key => $value) {
            	//$deviceList[$key]['taocan']=$this->Commission_m->getLastCommission($value['device_sn']);
            	$deviceList[$key]['taocan']=$this->Order_m->getLastOrder($value['device_sn']);
            	if($value['agent2']){
            		$agent2=$this->Agent_m->select(array('id'=>$value['agent2']));
            		$deviceList[$key]['agent2name']=$agent2[0]['agent_name'];
            	}else{
            		$deviceList[$key]['agent2name']='一级代理激活';
            		$deviceList[$key]['fptime']=$value['experience_time'];
            	}
            }  
        }      
        $data['deviceList']=$deviceList;
        echo _echo($data);
        exit;
	}




    //判断是否带正确的登录参数
    function islogin(){
        $tooken = $this->input->get('tooken');
        //没接收到参数
        if(empty($tooken)){
            $data['success'] = error('4008');
            echo _echo($data);
            exit;
        }else{
            //解密解不出来
            $adminid=jiemi($tooken);
            if(empty($adminid)){
                $data['success'] = error('4008');
                echo _echo($data);
                exit;
            }
            //没有查到该管理员
            $admin=$this->Admin_m->select(array('admin_id'=>$adminid));
            if(empty($admin)){
                $data['success'] = error('4008');
                echo _echo($data);
                exit;
            }
            return $admin;
        }
    }


	/**
     * 总后台设备列表
     * @param tooken     代理临时通行证
     * @param page       页码
     * @param limit      每页条数
     * @param device_sn   设备编号
     * @param jihuo      设备是否激活  0未激活 1已激活 不传返回全部
     * @param fenpei     设备是否分配给了一级代理  0未分配 1已分配 不传返回全部
     * @param experience 设备是否使用  0未使用 1已使用 不传返回全部
     */
	public function getAllDevice(){
		$this->islogin();
        $page=$this->input->get('page');
        $limit=$this->input->get('limit');
        // $page=1;
        // $limit=10;
        // $fenpei='1';
        if(empty($page)||empty($limit)){
            $data['success'] = error('4008');
            echo _echo($data);
            exit;
        }
        $device_sn = $this->input->get('device_sn');
        if($device_sn){
        	$where['device_sn']=$device_sn;
        }
        $jihuo = $this->input->get('jihuo');
        if($jihuo==='0'){
        	$where['jihuo']=0;
        }elseif($jihuo==='1') {
        	$where['jihuo']=1;
        }
        $fenpei = $this->input->get('fenpei');        
        if($fenpei==='0'){
        	$where['agent1']=null;
        }elseif($fenpei==='1') {
        	$where['agent1 > ']=0;
        }
        $experience = $this->input->get('experience');
        if($experience==='0'){
        	$where['experience']=0;
        }elseif($experience==='1') {
        	$where['experience']=1;
        }
        $offset=($page-1)*$limit;
        $orderby=' device_id desc ';
        $deviceList=$this->Device_m->select($where,$orderby,$limit,$offset);
        if($deviceList){
        	$data['deviceList']=$deviceList;
        }
        $data['success'] = success('4000');
        echo _echo($data);
        exit;
	}


	/**
     * 总后台批量分配设备给某个一级代理
     * @param tooken     		临时通行证
     * @param agentid     		代理id
     * @param devicesnList     	设备编号列表
     */
    public function deviceToAgent1(){
    	$admin=$this->islogin();
    	$agent1 = $this->input->post('agentid');
        $devicesnList = $this->input->post('devicesnList');
        if(empty($tooken)||empty($agent1)||empty($devicesnList)){
            $data['success'] = error('4008');
            echo _echo($data);
            exit;
        }
        foreach ($devicesnList as $key => $value) {
        	$device=$this->Device_m->select(array('device_sn'=>$value));
        	if($device[0]['agent1']){
        		$result[$value]=false;
        	}else{
        		$this->Device_m->update(array('device_sn'=>$value),array('agent1'=>$agent1));
        		$result[$value]=true;
        	}        	
        }        
        $Device_data['success'] = success('4000');	  
		$Device_data['result'] = $result;
		//分配设备给一级代理日志
		$log['admin_id']=$admin[0]['admin_id'];
		$log['log_info']='分配设备给一级代理(id:'.$agent1.')'.json_encode($result);
		$log['log_ip']=$_SERVER["REMOTE_ADDR"];
		$log['log_time']=time();
    	$this->Adminlog_m->insert($log);  
	    echo _echo($Device_data);
	    exit;
    }


	/**
     * 总后台设备详情
     * @param tooken     代理临时通行证
     * @param device_sn   设备编号
     */
	public function getDeviceinfoByDevicesn(){
		//$this->islogin();
		$device_sn = $this->input->get('device_sn');
		$device_sn = '866666030863584';
        if($device_sn){
        	$where['device_sn']=$device_sn;
        	$device=$this->Device_m->select($where);
        	$data=$this->chaxun_to_device($device_sn);
        	var_dump($device);
        	var_dump($data);
        }else{
        	$data['success'] = error('4008');
            echo _echo($data);
            exit;
        }
	}

	/**
     * 总后台修改设备详情信息
     * @param tooken get 代理临时通行证
     * @param device_sn  设备编号
     * @param agent1	 一级代理
     * @param agent2	 二级代理
     */
	public function updateDeviceinfoByDevicesn(){
		$admin=$this->islogin();
		$device_sn = $this->input->post('device_sn');
		//$device_sn = '866666030863584';
		$agent1 = $this->input->post('agent1');
		$agent2 = $this->input->post('agent2');
		if($device_sn){
			$Deviceinfo=array();
			if($agent1){
				$Deviceinfo['agent1']=$agent1;
			}
			if($agent2){
				$Deviceinfo['agent2']=$agent2;
			}
        	$where['device_sn']=$device_sn;
        	$this->Device_m->update($where,$Deviceinfo);
        	//修改设备详情信息日志
			$log['admin_id']=$admin[0]['admin_id'];
			$log['log_info']='修改单个设备(sn:'.$device_sn.')'.json_encode($Deviceinfo);
			$log['log_ip']=$_SERVER["REMOTE_ADDR"];
			$log['log_time']=time();
	    	$this->Adminlog_m->insert($log);  
        	$data['success'] = success('4000');
	        echo _echo($data);
	        exit;
        }else{
        	$data['success'] = error('4008');
            echo _echo($data);
            exit;
        }
	}










}


